//
//  ASBirthSelectSheet.m
//  ASBirthSheet
//
//  Created by Ashen on 15/12/8.
//  Copyright © 2015年 Ashen. All rights reserved.
//

#import "ASBirthSelectSheet.h"
#import <UIKit/UIKit.h>

static CGFloat MainScreenHeight = 0;
static CGFloat MainScreenWidth = 0;

#define ROW_HEIGHT 30
#define Component_Width (ScreenWidth - 20)/3

@interface ASBirthSelectSheet()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIPickerView *picker;

@property (nonatomic, strong) UIButton *btnDone;
@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) NSDateFormatter *formatter;

@property (nonatomic, assign) NSInteger selectPcaIndex;
@property (nonatomic, assign) NSInteger selectCityIndex;
@property (nonatomic, assign) NSInteger selectAreaIndex;

@end
@implementation ASBirthSelectSheet

- (instancetype)initWithFrame:(CGRect)frame type:(SheetType)type {
    if (self = [super initWithFrame:frame]) {
        MainScreenHeight = [UIScreen mainScreen].bounds.size.height;
        MainScreenWidth = [UIScreen mainScreen].bounds.size.width;
        [self setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.6]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickEmpty:)];
        [self addGestureRecognizer:tap];
        self.type = type;
        self.selectPcaIndex = 0;
        self.selectCityIndex = 0;
        self.selectAreaIndex = 0;
        
        [self makeUI];
    }
    return self;
}

- (void)makeUI {
    
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, MainScreenHeight, MainScreenWidth, 251)];
    _containerView.backgroundColor = [UIColor backgroundColor];
//    _containerView.layer.cornerRadius = 3;
//    _containerView.layer.masksToBounds = YES;
    switch (self.type) {
        case sheetTypeDate:
        {
            _datePicker =  [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 35, MainScreenWidth, 216)];
            [_datePicker setBackgroundColor:[UIColor whiteColor]];
            [_datePicker setDate:[NSDate date] animated:YES];
            //[_datePicker setMaximumDate:[NSDate date]];
            [_datePicker setDatePickerMode:UIDatePickerModeDate];
            
            [_datePicker setMinimumDate:[self.formatter dateFromString:@"1900-01-01"]];
            [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
            [_containerView addSubview:_datePicker];
        }
            break;
        case sheetTypePca:
        {
            _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 35, MainScreenWidth, 216)];
            [_picker setBackgroundColor:[UIColor whiteColor]];
            _picker.delegate = self;
            _picker.dataSource = self;
            
            [_picker selectRow:self.selectPcaIndex inComponent:0 animated:YES];
            [_picker selectRow:self.selectCityIndex inComponent:1 animated:YES];
            [_picker selectRow:self.selectAreaIndex inComponent:2 animated:YES];
            
            [_containerView addSubview:_picker];
        }
            break;
        default:
            break;
    }
    
    
    _btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnDone.frame = CGRectMake(MainScreenWidth-65, 0, 65, 35);
    [_btnDone setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
    [_btnDone setTitle:@"确定" forState:UIControlStateNormal];
    [_btnDone addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
//    _btnDone.layer.borderWidth = 0.3;
//    _btnDone.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [_containerView addSubview:_btnDone];
    
    _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnCancel.frame = CGRectMake(0, 0, 65, 35);
    [_btnCancel setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
    [_btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [_btnCancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:_btnCancel];
    
    [self addSubview:_containerView];
    
//    _containerView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
}

#pragma mark - private method
- (void)clickEmpty:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:0.3 animations:^{
        [self.containerView setFrame:CGRectMake(0, MainScreenHeight, MainScreenWidth, 251)];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)showPickerView {
    [UIView animateWithDuration:0.3 animations:^{
        [self.containerView setFrame:CGRectMake(0, MainScreenHeight - 251, MainScreenWidth, 251)];
    }];
}
#pragma mark - Action
- (void)doneAction:(UIButton *)btn {
    if (self.GetSelectDate) {
        if (self.type == sheetTypeDate) {
            _GetSelectDate([self.formatter stringFromDate:_datePicker.date],@"");
        } else {
            if (TextIsNotNil([self getAreaModel].aid)) {
                _GetSelectDate([self getAreaModel].aid,[NSString stringWithFormat:@"%@%@%@",[self getPcaModel].pname,[self getCityModel].cname,[self getAreaModel].aname]);
            } else if (TextIsNotNil([self getCityModel].cid)) {
                _GetSelectDate([self getCityModel].cid,[NSString stringWithFormat:@"%@%@",[self getPcaModel].pname,[self getCityModel].cname]);
            } else if (TextIsNotNil([self getPcaModel].pid)) {
                _GetSelectDate([self getPcaModel].pid,[self getPcaModel].pname);
            }
        }
        [self clickEmpty:nil];
    }
}

- (void)cancelAction:(UIButton *)btn {
    [self clickEmpty:nil];
}

- (void)dateChange:(id)datePicker {
    
}

- (PcaModel *)getPcaModel {
    return self.pcaArray[self.selectPcaIndex];
}
- (CityModel *)getCityModel {
    return self.pcaArray[self.selectPcaIndex].citys[self.selectCityIndex];
}
- (AreaModel *)getAreaModel {
    return self.pcaArray[self.selectPcaIndex].citys[self.selectCityIndex].areas[self.selectAreaIndex];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
    // 返回3列
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return self.pcaArray.count;
        case 1:
            return [self getPcaModel].citys.count;
        case 2:
            return [self getCityModel].areas.count;
        default:
            return 0;
    }
}
#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    // 设置每列的宽度
    return Component_Width;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    // 设置没行的高度
    return ROW_HEIGHT;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    if (component == 0) { // 省份
        if (!view) {
            view = [[UIView alloc] init];
        }
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(0, 0, Component_Width, ROW_HEIGHT);
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.text = self.pcaArray[row].pname;
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textColor = [UIColor blackColor];
        [view addSubview:textLabel];
        return view;
    }
    else if (component == 1){ // 城市
        if (!view) {
            view = [[UIView alloc] init];
        }
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(0, 0, Component_Width, ROW_HEIGHT);
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.text = self.pcaArray[self.selectPcaIndex].citys[row].cname;
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textColor = [UIColor blackColor];
        [view addSubview:textLabel];
        return view;
    }
    else{ // 区域
        if (!view) {
            view = [[UIView alloc] init];
        }
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(0, 0, Component_Width, ROW_HEIGHT);
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.text = self.pcaArray[self.selectPcaIndex].citys[self.selectCityIndex].areas[row].aname;
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textColor = [UIColor blackColor];
        [view addSubview:textLabel];
        return view;
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    switch (component) {
        case 0:{ // 选择省份
            self.selectPcaIndex = row;
            [pickerView reloadComponent:1]; // 重载城市
            [pickerView selectRow:0 inComponent:1 animated:YES];
            
            // 选择了省份就自动定位到该省的第一个市
            self.selectCityIndex = 0;
            // 选择了省份就自动定位到该省的第一个市的第一个区
            [pickerView  reloadComponent:2]; // 重载区域
            [pickerView selectRow:0 inComponent:2 animated:YES];
            
            // 海南 有的市没有区
            self.selectAreaIndex = 0;
//            if ([self.chinaModel getAreaDataByParentID:self.selectedCityID].count > 0) {
//                self.chinaModel.areaModel = (AreaModel *)[self.chinaModel getAreaDataByParentID:self.selectedCityID][0];
//            }
//            else{
//                self.chinaModel.areaModel.NAME = @"";
//                self.chinaModel.areaModel.ID = @"";
//                self.chinaModel.areaModel.PARENT_AREA_ID = @"9";
//                self.chinaModel.areaModel.GRADE = @"3";
//            }
            break;
        }
        case 1:{ // 选择城市
            self.selectCityIndex = row;
            // 选择了省份就自动定位到该省的第一个市
            self.selectAreaIndex = 0;
            // 选择了省份就自动定位到该省的第一个市的第一个区
            [pickerView  reloadComponent:2]; // 重载区域
            [pickerView selectRow:0 inComponent:2 animated:YES];
            
            break;
        }
        case 2: // 选择区域
            // 海南 有的市没有区  真是坑啊
            self.selectAreaIndex = row;
            
            break;
        default:
            break;
    }
}
#pragma mark - setter、getter
- (void)setSelectDate:(NSString *)selectDate {
    [_datePicker setDate:[self.formatter dateFromString:selectDate] animated:YES];
}
- (void)setPcaArray:(NSArray *)pcaArray {
    if (pcaArray) {
        _pcaArray = pcaArray;
        [self.picker reloadAllComponents];
    }
}

- (NSDateFormatter *)formatter {
    if (_formatter) {
        return _formatter;
    }
    _formatter =[[NSDateFormatter alloc] init];
    [_formatter setDateFormat:@"yyyy-MM-dd"];
    return _formatter;
    
}

@end
